import {LogicFlow} from '@logicflow/core'
import {Control, DndPanel, Menu, SelectionSelect, Snapshot} from "@logicflow/extension";
import {register} from "./myNode/UserTaskNode"
import {register as exclusiveGatewayRegister} from "./myNode/ExclusiveGatewayNode"
import {register as parallelGatewayRegister} from "./myNode/ParallelGatewayNode"
import {register as startRegister} from "./myNode/StartNode"
import {register as endRegister} from "./myNode/EndNode"
import {register as edgeRegister} from "./myNode/CurvedEdgeLine"
import {FlowNodeTypeEnum} from "@/enums/FlowNodeTypeEnum"

// 自定义拖拽面板
function getPatternItems(lf: LogicFlow): any[] {
    return [
        {
            label: '选区',
            icon: '',
            callback: () => {
                lf.openSelectionSelect();
                lf.once('selection:selected', () => {
                    lf.closeSelectionSelect();
                });
            }
        },
        {
            type: FlowNodeTypeEnum.FLOW_START,
            text: '开始',
            nodeType: 'START',
            label: '开始节点',
            icon: '',
        },
        {
            type: FlowNodeTypeEnum.USER_TASK,
            label: '用户任务',
            text: '用户任务',
            icon: '',
            className: 'important-node',
            properties: {
                nodeType: 'USER_TASK',
                assignee: '',
                candidateUsers: '',
                formKey: '',
                nodeName: '用户任务',
                taskListener: '',
                targetRef: ''
            }
        },
        {
            type: FlowNodeTypeEnum.EXCLUSIVE_GATEWAY,
            label: '排他网关',
            nodeType: 'EXCLUSIVE_GATEWAY',
            icon: '',
        },
        {
            type: FlowNodeTypeEnum.PARALLEL_GATEWAY,
            label: '并行网关',
            nodeType: 'PARALLEL_GATEWAY',
            icon: '',
        },
        {
            type: FlowNodeTypeEnum.FLOW_END,
            nodeType: 'END',
            label: '结束节点',
            icon: '',
        }
    ]
}

function initLogicFlow(): LogicFlow {
    // 注册扩展插件
    LogicFlow.use(SelectionSelect)
    LogicFlow.use(Control)
    LogicFlow.use(DndPanel)
    LogicFlow.use(Snapshot)//导出图片
    LogicFlow.use(Menu)
    // 创建 LogicFlow 实例
    const lf = new LogicFlow({
        container: document.getElementById('container')!, // 指定容器元素
        style: {
            baseNode: {
                fill: '#fff', // 边的填充颜色
            },
            rect: {
                rx: 5, // 矩形节点的圆角 x 半径
                ry: 5, // 矩形节点的圆角 y 半径
                stroke: '#6692CC',
            },
            circle: {
                stroke: '#6692CC',
            },
            polygon: {
                stroke: '#6692CC',
            },
            baseEdge: {
                stroke: '#6692CC',
            },
        },
        grid: true, // 启用网格
        edgeType: FlowNodeTypeEnum.FLOW_EDGE, // 设置 LogicFlow 使得所有连线默认使用自定义边
    });
    // 自定义组件注册
    register(lf)
    exclusiveGatewayRegister(lf)
    parallelGatewayRegister(lf)
    startRegister(lf)
    endRegister(lf)
    edgeRegister(lf)
    // 自定义拖拽面板
    lf.setPatternItems(getPatternItems(lf))
    return lf;
}

export default initLogicFlow;
